package com.example.mynotification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;

public class MainActivity extends AppCompatActivity {
    private NotificationManager manager;
    private Notification notification;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            NotificationChannel channel=new NotificationChannel("leo","测试通知",NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
        Intent intent=new Intent(this,NotificationActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
        notification=new NotificationCompat.Builder(this,"leo")
                .setContentTitle("官方通知")//标题
                .setContentText("CSDN博客碰磕邀你一起学习~")//内容
                .setSmallIcon(R.drawable.ic_person_outline_black_24dp)//小图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.chunyu02))//大图标
                .setColor(Color.parseColor("#ff0000"))//图标颜色
                .setContentIntent(pendingIntent) //点击跳转后的意图
                .setAutoCancel(true)//点击通知后会取消通知
                .build();
    }
    public void sendNotification(View view){
            manager.notify(1,notification);
    }
    public void cacelNotification(View view){
            manager.cancel(1);
    }
}
